#ifndef SC_FPSCOUNTER_H
#define SC_FPSCOUNTER_H

#include "common.h"

#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>

// atomic_bool 定义在下面的头文件中，为什么没有引用此头文件,引用了为什么还会报错？
// #include <atomic>

#include "util/thread.h"

/*
FPS是图像领域中的定义，是指画面每秒传输帧数，通俗来讲就是指动画或视频的画面数。
FPS是测量用于保存、显示动态视频的信息数量。每秒钟帧数越多，所显示的动作就会越流畅。
通常，要避免动作不流畅的最低是30。
*/
// FPS计数器
struct sc_fps_counter {
    sc_thread thread;
    sc_mutex mutex;
    sc_cond state_cond;

    bool thread_started;

    // atomic so that we can check without locking the mutex
    // if the FPS counter is disabled, we don't want to lock unnecessarily
    atomic_bool started;

    // the following fields are protected by the mutex
    bool interrupted;

    // 每秒渲染的帧数
    unsigned nr_rendered;
    // 每秒跳过的帧数    
    unsigned nr_skipped;
    sc_tick next_timestamp;
};

/*
懒加载初始化
@param counter FPS计数器
@return 成功返回true,否则返回false
*/
bool
sc_fps_counter_init(struct sc_fps_counter *counter);

/*
回收资源
@param counter
*/
void
sc_fps_counter_destroy(struct sc_fps_counter *counter);

/*
异步启动
@param counter
@return 
*/
bool
sc_fps_counter_start(struct sc_fps_counter *counter);

void
sc_fps_counter_stop(struct sc_fps_counter *counter);

bool
sc_fps_counter_is_started(struct sc_fps_counter *counter);

// request to stop the thread (on quit)
// must be called before sc_fps_counter_join()
void
sc_fps_counter_interrupt(struct sc_fps_counter *counter);

void
sc_fps_counter_join(struct sc_fps_counter *counter);

void
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter);

void
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter);

#endif
